package com.taiyuan.tongcheng.ui;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpConfig;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.utils.KJLoger;
import org.kymjs.kjframe.utils.SystemTool;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.taiyuan.tongcheng.bean.DriverPosition;
import com.taiyuan.tongcheng.http.HttpParamUtil;
import com.taiyuan.tongcheng.http.TcWebservice;
import com.taiyuan.tongcheng.http.WebData;
import com.taiyuan.tongcheng.jsonparser.TcJsParser;
import com.taiyuan.tongcheng.util.ACache;
import com.taiyuan.tongcheng.util.L;
import com.tytc56.user.R;

public class TruckerPositionActivity extends BaseActivity{
	//==widget
	@BindView(id=R.id.btn_get_positon,click=true)
	private Button postionBtn;
	@BindView(id=R.id.tv_current_positon)
	private TextView postionInfo;
	//==获取位置信息
	public static final String TAG_DRIVER_ID="driver_id";
	private String driverId="";
	
	//== 定位相关
	@BindView(id=R.id.mapview_driver_info)
	private MapView  baiduMapView ;
	private BaiduMap  mapControl;
	private GeoCoder geoCoder;
	BitmapDescriptor bitMapDes;
	//== 缓存
	private ACache acache;
	
	
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_driver_position);
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.btn_get_positon:
			if(checkNet()){
				TcLoaddingDialog.show();
				getDriverPosition(driverId);
			}
			break;

		default:
			break;
		}
	}
	
	@Override
	public void initWidget() {
		super.initWidget();
		initView();
		topTv.setText(R.string.top_title_wuliu);
		rightTv.setVisibility(View.GONE);
		if(checkNet()){
			TcLoaddingDialog.show();
			getDriverPosition(driverId);
		}
	}
	
	@Override
	public void initData() {
		super.initData();
		acache=ACache.get(getApplicationContext());
		driverId=getIntent().getStringExtra(TAG_DRIVER_ID);
		initMap();
	
	}
	
	/**
	 * 
	* @Description: 检查网络状况
	* @return
	* @return boolean
	* @throws
	* @autour wzq
	* @date 2015-9-21 下午1:09:33
	* @update (date)
	 */
	private boolean  checkNet() {
		if(SystemTool.checkNet(getApplicationContext())){
			return true;
		}else{
			return false;
		}
	}
	
	
	
	private void initMap() {
		// 初始化全局 bitmap 信息，不用时及时 recycle
        bitMapDes = BitmapDescriptorFactory
				.fromResource(R.drawable.map_pin);
		mapControl=baiduMapView.getMap();
		// 初始化搜索模块，注册事件监听
		geoCoder = GeoCoder.newInstance();
		baiduMapView.showScaleControl(false);
		baiduMapView.showZoomControls(false);
		
		if(acache.getAsObject("Latitude")!=null){
			 LatLng cenpt = new LatLng((Double)acache.getAsObject("Latitude"),(Double)acache.getAsObject("Longitude")); 
		        //定义地图状态
			 MapStatus mMapStatus = new MapStatus.Builder()
		        .target(cenpt)
		        .zoom(16)
		        .build();
		        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
		        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
		    	mapControl.animateMapStatus(mMapStatusUpdate);
		}else{
			MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(16);
			mapControl.animateMapStatus(u);
		}
		
		
		L.v("证书", SystemTool.getSign(getApplicationContext(), "com.tytc56.user"));
	}
	
	
	/**
	 * 
	* @Description: 获取司机信息
	* @param driverId
	* @return void
	* @throws
	* @autour wzq
	* @date 2015-9-21 下午1:58:59
	* @update (date)
	 */
	private void getDriverPosition(String driverId) {

		HttpConfig config = new HttpConfig();
		config.cacheTime = 0;
		KJHttp kjHttp = new KJHttp(config);
        HttpParams params =new HttpParams();
        params=HttpParamUtil.getDriverPosition(getApplicationContext(), driverId);
        KJLoger.debug(params.getUrlParams().toString());
        kjHttp.post(TcWebservice.WEBSERVER_IP+TcWebservice.API.GET_DRIVER_POSITION, params, new HttpCallBack() {
			@Override
			public void onFailure(int errorNo, String strMsg) {
				super.onFailure(errorNo, strMsg);
			}
			@Override
			public void onSuccess(String t) {
				try {
					String date=URLDecoder.decode(t, "UTF-8");
					KJLoger.debug(date);
					WebData webdata = TcJsParser.getWedataData(date); 
					if(webdata.getStatus()==1){
						DriverPosition  postion =JSON.parseObject(webdata.getData(), DriverPosition.class);
					    drawPositonBaiduMap(postion);
					}else{
						showErroCustomToast(webdata.getMsg());
					}
					 if(TcLoaddingDialog!=null&&TcLoaddingDialog.isShowing()){
						  TcLoaddingDialog.cancel();
					  }
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				super.onSuccess(t);
			}
		
        });
	
	}
	/**
	 * 
	* @Description: 在百度地图上绘制
	* @return void
	* @throws
	* @autour wzq
	* @date 2015-9-21 下午1:14:43
	* @update (date)
	 */
	private void drawPositonBaiduMap(DriverPosition postion) {
		
		postionInfo.setText(postion.getPlace());
		
		
		MapStatus mMapStatus = new MapStatus.Builder()
				.target(new LatLng(postion.getLatitude(), postion
						.getLongitude())).zoom(17).build();
		// 定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
		MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
				.newMapStatus(mMapStatus);
		// 改变地图状态
		mapControl.setMapStatus(mMapStatusUpdate);

		OverlayOptions oo = new MarkerOptions()
				.position(
						new LatLng(postion.getLatitude(), postion
								.getLongitude())).icon(bitMapDes).zIndex(5);
		mapControl.addOverlay(oo);
	}
}
